package com.example;

import org.junit.Test;

import java.io.*;
import java.util.Arrays;

/**
 * 字节流
 */
public class IOTest01 {

    @Test
    public void test01() {
        InputStream is = null;
        try {
            is = new
                    FileInputStream("F:/课堂/Java面向对象编程基础/data/a.txt");
            int read = is.read();
            System.out.println((char)read); // 49->1
            read = is.read();
            System.out.println((char)read); // 50->2
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                // 可能抛空指针或IO异常
                is.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    @Test
    public void test02() throws Exception {
        InputStream is = new
                FileInputStream("F:/课堂/Java面向对象编程基础/data/a.txt");
        /* 字节数组通常定义成1024的整数倍 */
        byte[] bytes = new byte[1024];
        System.out.println(Arrays.toString(bytes)); // 空数组
        // 执行读取方法后bytes的内部元素就会赋值
        int read = is.read(bytes);
        System.out.println(read); // 89
        // 内容被读完了
        //read = is.read(bytes);
        // System.out.println(read); // -1，没有内容
        System.out.println(Arrays.toString(bytes)); // 部分赋值
        String s = new String(bytes, 0, read);
        System.out.println(s);
        is.close();
    }

    @Test
    public void test03() throws Exception {
        OutputStream os = new
                FileOutputStream("F:/课堂/Java面向对象编程基础/data/c.txt");
        os.write(97); //a
        os.write(98); //b
        os.write(99); //c
        os.write("使用输出流写入文件".getBytes()); // 写一个字节数组
        os.flush();
        os.close();
    }

    /**
     * 复制：边读边写
     * @throws Exception
     */
    @Test
    public void test04() throws Exception {
        // 构建原始文件的输入流
        InputStream is = new
                FileInputStream("F:\\课堂\\Java面向对象编程基础\\data\\File类.mp4");
        // 构建新文件的输出流
        OutputStream os = new
                FileOutputStream("F:\\课堂\\Java面向对象编程基础\\data\\课堂练习1-3(副本).mp4");
        byte[] bytes = new byte[1024 * 1024];
        int length = 0;
        while ((length = is.read(bytes)) != -1) {
            os.write(bytes, 0, length); // 最后一次length可能不足1024 * 5
        }
        os.close();
        is.close();
    }

    /* 使用缓冲流进行高效读写 */
    @Test
    public void test05() throws Exception {
        /* 先定义普通的字节输入流 */
        InputStream is = new
                FileInputStream("F:\\课堂\\Java面向对象编程基础\\data\\File类.mp4");
        /* 转换成高效的缓冲字节输入流 */
        BufferedInputStream bis = new BufferedInputStream(is);
        OutputStream os = new
                FileOutputStream("F:\\课堂\\Java面向对象编程基础\\data\\课堂练习1-3(副本2).mp4");
        /* 转换成高效的缓冲字节输出流 */
        BufferedOutputStream bos = new BufferedOutputStream(os);
        byte[] buffer = new byte[1024 * 1024];
        int length = 0;
        while (( length = bis.read(buffer) ) != -1) {
            bos.write(buffer, 0, length);
        }
        bos.close();
        os.close();
        bis.close();
        is.close();
    }
}
